package com.hhkj.admin.sale.controller;

import com.github.pagehelper.PageInfo;
import com.hhkj.admin.common.util.WechatAppUtil;
import com.hhkj.admin.order.common.cont.OrderConst;
import com.hhkj.admin.sale.common.component.supplier.SaleSupplier;
import com.hhkj.admin.sale.common.cont.SaleConst;
import com.hhkj.admin.sale.domain.Sale;
import com.hhkj.admin.sale.model.dopo.SalePo;
import com.hhkj.admin.sale.model.req.SaleReq;
import com.hhkj.admin.sale.model.res.SaleManagementRes;
import com.hhkj.admin.sale.model.res.SaleRes;
import com.hhkj.admin.sale.service.biz.SaleBizService;
import com.ruoyi.common.annotation.PermissionPrefix;
import com.ruoyi.common.base.biz.controller.v2.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.valid.VPage;
import com.ruoyi.system.domain.SysLogininfor;
import org.apache.poi.ss.usermodel.Workbook;
import org.bouncycastle.cert.ocsp.Req;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@RequestMapping("/sale")
@PermissionPrefix(value = PermissionPrefix.NO_PERMISSION)
public class SaleController extends BaseController<Sale, SaleReq, SaleRes, SaleBizService> {

    @Autowired
    private SaleSupplier saleSupplier;

    @PostMapping("/saleRoot/add")
    public AjaxResult saveSaleRoot(@RequestBody SaleReq saleReq) {
        return toAjax(this.getBizService().saveSaleRoot(saleReq));
    }

    @PutMapping("/reset/{id}")
    public AjaxResult reset(@PathVariable("id") Long id) {
        return toAjax(this.getBizService().reset(id));
    }

    @GetMapping("/invitation/code/url/{id}")
    public AjaxResult getInvitationCode(@PathVariable("id") Long id) {
        return AjaxResult.success(saleSupplier.getInvitationCodeUrlById(id));
    }

    @PostMapping("/management/paging")
    public TableDataInfo managementPaging(@Validated(VPage.class) @RequestBody SaleReq saleReq) {
        startPageBody(saleReq);
        List<SaleManagementRes> saleResList = getBizService().listForManagement(saleReq);
        PageInfo<SaleManagementRes> salePoPageInfo = new PageInfo<>(saleResList);
        return getDataTable(salePoPageInfo);
    }

    @PutMapping("/management/disable")
    public AjaxResult disable(@RequestBody SaleReq saleReq) {
        return AjaxResult.success(getBizService().updateDisabledFlag(saleReq));
    }

    @PostMapping("/management/export")
    public void saleManagementExport(@RequestBody SaleReq saleReq, HttpServletResponse response) {
        ExcelUtils.downLoadExcel(SaleConst.SALE_MANAGEMENT_EXPORT_TITLE, response, this.getBizService().createSaleManagementExportExcel(saleReq));
    }
}
